<template>
  <div style="height: 6vh;background-color:#fff;padding: 0 20px 10px;">
    <span style="line-height:60px;font-size:20px;">车辆管理</span>
    <el-button type="primary" style="float:right;margin-top:12px;" @click='beforeAddVehicle'>添加车辆</el-button>
  </div>
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.brand"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.license"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆型号">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.model"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.code"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="车辆排量">
              <el-select placeholder="请选择" v-model="saveVehicleForm.displacement">
                  <el-option label="1.6" value="1" />
                  <el-option label="2.5" value="2" />
                  <el-option label="4" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆类型">
              <el-select placeholder="请选择" v-model="saveVehicleForm.type">
                <el-option label="轿车" value="10"></el-option>
                <el-option label="客车" value="20"></el-option>
                <el-option label="货车" value="30"></el-option>
                <el-option label="挂车" value="40"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车身颜色">
            <el-select placeholder="请选择" v-model="saveVehicleForm.color">
              <el-option label="黑" value="10"></el-option>
              <el-option label="白" value="20"></el-option>
              <el-option label="蓝" value="30"></el-option>
              <el-option label="灰" value="40"></el-option>
              <el-option label="银" value="50"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="里程数">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.kilometers"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
            v-model="saveVehicleForm.buyTime" @change="validateDate"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上牌时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
            v-model="saveVehicleForm.regTime" @change="validateDate"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input placeholder="请输入内容" v-model="saveVehicleForm.price"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池类型">
            <el-select placeholder="请选择" v-model="saveVehicleForm.batteryType">
              <el-option label="铅酸蓄电池" value="10"></el-option>
              <el-option label="镍氢电池" value="20"></el-option>
              <el-option label="钠硫电池" value="30"></el-option>
              <el-option label="二次锂电池" value="40"></el-option>
              <el-option label="空气电池" value="50"></el-option>
              <el-option label="三元锂电池" value="60"></el-option>
              <el-option label="碱性燃料电池" value="70"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveVehicle">确定</el-button>
    </template>
  </el-dialog>
  <el-card style="margin: 10px 0; height: 70px;">
    <el-form :inline="true">
      <el-form-item label="车辆品牌" style="width:270px;">
        <el-input placeholder="请输入内容" v-model="searchVehicleForm.brand"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" style="width:270px;">
        <el-input placeholder="请输入内容" v-model="searchVehicleForm.license"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadVehicle">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:10px 0;">
    <el-table :data="vehicleArr">
      <el-table-column type="index" label="编号" width="60" align="center"/>
      <el-table-column prop="brand" label="车辆品牌" align="center"/>
      <el-table-column prop="license" label="车牌号" align="center"/>
      <el-table-column prop="code" label="车辆识别码" align="center"/>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormatter"/>
      <el-table-column prop="price" label="购买价格" align="center"/>
      <el-table-column prop="buyTime" label="购买时间" align="center"/>
      <el-table-column prop="regTime" label="上牌时间" align="center"/>
      <el-table-column prop="batteryType" label="电池类型" align="center" :formatter="batteryTypeFormatter"/>
      <el-table-column prop="status" label="车辆状态" align="center" :formatter="vehicleStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="editVehicle(scope.row.id)">编辑</el-button>
          <el-button link type="primary" @click="deleteVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import axios from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus';
import qs from 'qs';
import { onMounted, ref } from 'vue';

const vehicleArr = ref([]);
const dialogVisible = ref(false);
const dialogTitle = ref('');
const searchVehicleForm = ref({
  brand: '',
  license: ''
});
const loadVehicle = () => {
  let data = qs.stringify(searchVehicleForm.value);
  axios.get(BASE_URL + '/v1/vehicle/list?' + data).then(res=>{
    if(res.data.code == 2000) {
      vehicleArr.value = res.data.data;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
onMounted(()=>{
  loadVehicle();
});
const resetSearch = () => {
  searchVehicleForm.value = {};
  loadVehicle();
};
const saveVehicleForm = ref({
  brand: '',
  license: '',
  model: '',
  code: '',
  displacement: '',
  type: '',
  color: '',
  kilometers: '',
  price: '',
  buyTime: '',
  regTime: '',
  batteryType: ''
});
const validateDate = () => {
  const buyTime = new Date(saveVehicleForm.value.buyTime);
  const regTime = new Date(saveVehicleForm.value.regTime);
  if(buyTime && regTime && buyTime >= regTime) {
    ElMessage.error('上牌时间不能大于购买时间');
    saveVehicleForm.value.regTime = '';
  }
};
const handleClose = () => {
  if(confirm('确定取消保存车辆吗')){
    dialogVisible.value = false;
    saveVehicleForm.value = {};
  }
};
const beforeAddVehicle = () => {
  dialogTitle.value = '添加车辆';
  dialogVisible.value = true;
};
const saveVehicle = () => {
  let  data = qs.stringify(saveVehicleForm.value);
  axios.post(BASE_URL + '/v1/vehicle/save',data).then(res=>{
    if(res.data.code == 2000) {
      ElMessage.success('保存成功');
      saveVehicleForm.value = {};
      dialogVisible.value = false;
      loadVehicle();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const editVehicle = (id) => {
  dialogTitle.value = '编辑车辆';
  dialogVisible.value = true;
  axios.get(BASE_URL + '/v1/vehicle/list?id=' + id).then(res=>{
    if(res.data.code == 2000) {
      saveVehicleForm.value = res.data.data[0];
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const deleteVehicle = (id) => {
  ElMessageBox.confirm('此操作将永久删除该车辆, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    axios.post(BASE_URL + '/v1/vehicle/delete/' + id).then(res=>{
      if(res.data.code == 2000) {
        ElMessage.success('删除成功');
        loadVehicle();
      } else {
        ElMessage.error(res.data.msg);
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除'
    });
  });
};
</script>

<style scoped>

</style>